// pages/myCenter/makeOutInvoice/makeOutInvoice.js
import Common from '../../../common/common.js';
let common = new Common;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    companyName: '',//公司名称
    ein: '',//税号
    openNlank: '',//开户行
    bankAccount: '',//开户行账号
    companyTel: '',//公司电话
    companyAddress: '',//公司地址
    consignee: '',//收货人
    phone: '',//手机号
    addressArea: '',//收货地区
    addressDetile: '',//详细收货地址
    price: '',//开票金额
    serviceCharge: '',//手续费
    list: [],//接收上个页面选中开票的id
  },
  // 省市区联动
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let addressArea = e.detail.value[0] + e.detail.value[1] + e.detail.value[2];
    this.setData({
      //   region: e.detail.value
      addressArea: addressArea
    })
  },
  // 公司名称
  company: function (e) {
    console.log(e)
    this.setData({
      companyName: e.detail.value
    })
  },

  // 税号
  ein: function (e) {
    this.setData({
      ein: e.detail.value
    })
  },
  // 开户行
  openNlank: function (e) {
    this.setData({
      openNlank: e.detail.value
    })
  },
  // 开户行账号
  bankAccount: function (e) {
    this.setData({
      bankAccount: e.detail.value
    })
  },
  // 公司电话
  companyTel: function (e) {
    this.setData({
      companyTel: e.detail.value
    })
  },
  // 公司地址
  companyAddress: function (e) {
    this.setData({
      companyAddress: e.detail.value
    })
  },
  // 收货人 
  consignee: function (e) {
    this.setData({
      consignee: e.detail.value
    })
  },
  // 手机号
  phone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 收货地址
  addressDetile: function (e) {
    this.setData({
      addressDetile: e.detail.value
    })

  },
  submit: function (e) {
    console.log(this.data.companyName)
    if (!this.data.companyName) {
      wx.showToast({
        title: '请填写公司名称',
        icon: 'none'
      })
      return
    }
    if (!this.data.ein) {
      wx.showToast({
        title: '请填写税号',
        icon: 'none'
      })
      return
    }
    if (!this.data.openNlank) {
      wx.showToast({
        title: '请填写开户行',
        icon: 'none'
      })
      return
    }
    if (!this.data.bankAccount) {
      wx.showToast({
        title: '请填写开户行账号',
        icon: 'none'
      })
      return
    }
    if (!this.data.consignee) {
      wx.showToast({
        title: '请填写收货人',
        icon: 'none'
      })
      return
    }
    if (!this.data.phone) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
      return
    }
    if (!this.data.addressArea) {
      wx.showToast({
        title: '请填写收货区域',
        icon: 'none'
      })
      return
    }
    if (!this.data.addressDetile) {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none'
      })
      return
    }
    this.addBillingRecord()
  },

  // 开发票接口
  addBillingRecord: function (e) {
    let arr = this.data.list.split(",")
    console.log(arr)
    let list = [];

    for (var i = 0; i < arr.length; i++) {
      list.push({ orderId: arr[i] })
    }
    console.log(list)
    let data = {
      "orgSeq": getApp().globalData.orgSeq,
      "dutyNumber": this.data.ein,//税号
      "openingBank": this.data.openNlank,//开户行
      "openingBankAccount": this.data.bankAccount,//开户行账号
      "companyName": this.data.companyName,//公司名称
      "companyTel": this.data.companyTel,//公司电话
      "companyAddress": this.data.companyAddress,//公司地址
      "receiver": this.data.consignee,//'收货人
      "receiverTel": this.data.phone,//手机号
      "receiverRegion": this.data.addressArea,//收货地区
      "receiverAddress": this.data.addressDetile,//详细收货地址
      "billingAmount": this.data.price,//开票金额
      "billingOrderShipDTOList": list,//选中的订单id
      "serviceCharge": this.data.serviceCharge,//手续费
    }
    console.log('data', data);
    common.requestAjax('/api/user-service/v1/billingRecord/addBillingRecord', 'POST', data).then(res => {
      wx.hideLoading();
      if (res.data.code == 1) {
        wx.showToast({
          title: '开票成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/myCenter/reconciliation/reconciliation?link=' + 0
          })
        }, 2000);
      }else{
        wx.showToast({
          title: res.data.message,
          icon:'none'
        })
      }
    }).catch(err => {
      common.showToast("系统异常，请稍后重试！");
    })
  },
  // 初始化数据
  init: function () {
    let data = {
      "size": 10,
      "current": 1,
      "condition": {
        "orgSeq": getApp().globalData.orgSeq,
      },
      "ascs": [],
      "descs": []
    }
    common.requestAjax('/api/user-service/v1/billingRecord/queryBillingRecord', 'POST', data).then(res => {
      if (res.data.code == 1) {
        console.log(res.data.records)
        if (!this.data.price) {
          this.setData({
            price: res.data.records[0].serviceCharge,//开票金额
          })
        } 
        this.setData({
          companyName: res.data.records[0].companyName,//公司名称
          ein: res.data.records[0].dutyNumber,//税号
          openNlank: res.data.records[0].openingBank,//开户行
          bankAccount: res.data.records[0].openingBankAccount,//开户行账号
          companyTel: res.data.records[0].companyTel,//公司电话
          companyAddress: res.data.records[0].companyAddress,//公司地址
          consignee: res.data.records[0].receiver,//收货人
          phone: res.data.records[0].receiverTel,//手机号
          addressArea: res.data.records[0].receiverRegion || "",//收货地区
          addressDetile: res.data.records[0].receiverAddress,//详细收货地址
        })
      }
    }).catch(err => {
    })
  },
  onLoad: function (options) {
    this.setData({
      list: options.list,
      serviceCharge: options.feeMoneyNum,
    })
    if (this.data.serviceCharge == 0) {
      wx.showToast({
        title: '开票金额为0，暂不自持开票',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateBack({})
      }, 2000);
      return
    }
    this.init()
  },
  onShow: function () {

  },
})